<!DOCTYPE html>
<html>
<head>   
    <title>jobs</title>   
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>          
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">                    
                    <el-form-item label="Job Name">
                        <el-input v-model="searchData.name" placeholder="Job name"></el-input>
                    </el-form-item>      
                    <el-form-item label="Job Type">
                        <el-input v-model="searchData.type" placeholder="Job type"></el-input>
                    </el-form-item>   
                    <el-form-item label="Job Schedule Status">
                        <el-select v-model="searchData.scheduleStatusList" multiple clearable placeholder="please select schedule status">
                            <el-option v-for="item in scheduleList"
                            :key="item.schedule"
                            :label="item.name"
                            :value="item.schedule">
                            </el-option>
                        </el-select>   
                    </el-form-item>     
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Create</el-button>
                    </el-form-item>
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="type" label="Type" min-width="7%"></el-table-column>       
                    <el-table-column prop="name" label="Job Name" min-width="12%"></el-table-column>
                    <el-table-column prop="cron" label="Cron" min-width="10%"></el-table-column>
                    <el-table-column prop="requestUrl" label="Url" min-width="20%"></el-table-column>
                    <el-table-column prop="requestMethod" label="Method" min-width="7%"></el-table-column>
                    <el-table-column prop="scheduleStrategyName" label="Strategy" min-width="8%"></el-table-column>      
                    <el-table-column label="Scheduled" min-width="8%">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.scheduleType">{{scope.row.scheduleStatusName}}</el-tag>
                        </template>
                    </el-table-column> 
                    <el-table-column label="Operation" min-width="25%">
                        <template slot-scope="scope">                                      
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                            <el-button @click="jobSchedule(scope.row.id)" type="text" size="small">运行一次</el-button>
                            <el-link type="text" :href="'./joblogs.htm?jobId='+scope.row.id+'&jobName='+scope.row.name" underline="false">&nbsp;&nbsp;日志</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[10, 15, 20]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form :model="dialogForm" :rules="dialogFormRules" ref="dialogForm" :label-width="formLabelWidth" size="mini">  
                    <el-row>
                        <el-col span="12">
                            <el-form-item label="Job Name" prop="name">
                                <el-input v-model="dialogForm.name" autocomplete="off" placeholder="please input name"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col span="12">
                            <el-form-item label="Job Type" prop="type">
                                <el-input v-model="dialogForm.type" autocomplete="off" placeholder="please input type"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>                  
                    <el-row>
                        <el-col span="12">
                            <el-form-item label="Schedule Cron" prop="cron">
                                <el-input v-model="dialogForm.cron" autocomplete="off"  placeholder="please input cron"></el-input>
                            </el-form-item>    
                        </el-col>
                        <el-col span="12">
                            <el-form-item label="Strategy">
                                <el-select v-model="dialogForm.scheduleStrategy" placeholder="please select status">
                                    <el-option v-for="item in strategyList"
                                    :key="item.strategy"
                                    :label="item.name"
                                    :value="item.strategy">
                                    </el-option>
                                </el-select>
                            </el-form-item> 
                        </el-col>
                    </el-row>                      
                    <el-row>
                        <el-col span="6">
                            <el-form-item label="Method">
                                <el-select v-model="dialogForm.requestMethod" placeholder="please select method">
                                    <el-option v-for="item in methodList"
                                    :key="item.method"
                                    :label="item.name"
                                    :value="item.method">
                                    </el-option>
                                </el-select>
                            </el-form-item>   
                        </el-col>
                        <el-col span="18">
                            <el-form-item label="RequestUrl" prop="requestUrl">
                                <el-input v-model="dialogForm.requestUrl" autocomplete="off" placeholder="lb://application-name/path" maxlength="1024" clearable>                                  
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col span="24">
                            <el-form-item label="RequestBody">
                                <el-input v-model="dialogForm.requestParams" autocomplete="off" type="textarea"  placeholder="please input requestParams"></el-input>
                            </el-form-item>   
                        </el-col>                       
                    </el-row>   
                    <el-row>
                        <el-col span="24">
                            <el-form-item label="Description">
                                <el-input v-model="dialogForm.description" autocomplete="off" type="textarea"  placeholder="please input alert description"></el-input>
                            </el-form-item>
                        </el-col>                       
                    </el-row>  
                    <el-row>                      
                        <el-col span="16">
                            <el-form-item label="AlertEmail">
                                <el-input v-model="dialogForm.alertEmail" autocomplete="off" placeholder="please input alert email"></el-input>
                            </el-form-item>   
                        </el-col>
                        <el-col span="8">
                            <el-form-item label="Status">
                                <el-select v-model="dialogForm.scheduleStatus" placeholder="please select schedule status">
                                    <el-option v-for="item in scheduleList"
                                    :key="item.schedule"
                                    :label="item.name"
                                    :value="item.schedule">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>                  
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="resetForm('dialogForm')">取 消</el-button>
                    <el-button type="primary" @click="editSubmit('dialogForm')">确 定</el-button>
                </div>                
              </el-dialog>
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();
        function docLoaded() {           
            var queryApp = new Vue({el: '#queryForm', data: {                                
                    searchData:{ 
                        name: "", 
                        type: "", 
                        scheduleStatusList:[],
                        pageNum:1,
                        pageSize:10
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:0,name:"Normal"},{status:1,name:"Disable"}],
                    methodList:[{method:"get",name:"GET"},{method:"post",name:"POST"},{method:"put",name:"PUT"},{method:"delete",name:"DELETE"}],
                    strategyList:[{strategy:0,name:"Default"},{strategy:1,name:"Once"}],
                    scheduleList:[{schedule:1,name:"Started"},{schedule:2,name:"Stoped"}],
                    formLabelWidth: '135px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{                      
                        id:0,
                        type:"",
                        name:"",  
                        cron:"",                      
                        requestUrl:"",
                        requestMethod:"get",
                        requestParams:"",
                        alertEmail:"",
                        scheduleStrategy:0, 
                        scheduleStatus:0,                     
                        description:""
                    },
                    dialogFormRules: {
                        type: [
                            { required: true, message: '请输入Job type', trigger: 'blur' },
                            { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' }
                        ],
                        name: [
                            { required: true, message: '请输入Job名称', trigger: 'blur' },
                            { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
                        ],
                        cron: [
                            { required: true, message: '请输入Job cron', trigger: 'blur' },
                            { min: 3, max: 64, message: '长度在 10 到 64 个字符', trigger: 'blur' }
                        ],
                        requestUrl: [
                            { required: true, message: '请输入Job 请求URL', trigger: 'blur' },
                            { min: 8, max: 512, message: '长度在 8 到 512 个字符', trigger: 'blur' }
                        ]
                    }
                },
                methods: {     
                    onSubmit:function(event){
                        this.searchData.pageNum = 1;
                        this.loadTableData(event);
                    },                     
                    loadTableData:function(event){                          
                        httpPost(apiJobPage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.loadTableData();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.loadTableData();
                    },                  
                    handleEdit(row){
                        this.dialogForm = {
                            id:row.id,
                            type:row.type,
                            name:row.name,  
                            cron:row.cron,
                            requestUrl:row.requestUrl,
                            requestMethod:row.requestMethod,
                            requestParams:row.requestParams,
                            alertEmail:row.alertEmail,
                            scheduleStrategy:row.scheduleStrategy,  
                            scheduleStatus:row.scheduleStatus,                            
                            description:row.description
                        };                       
                        this.dialogTitle = "Update Job";
                        this.dialogMode = 0;
                        this.editDialogFormVisible = true;
                    },                   
                    handleAdd(){
                        this.dialogForm = {
                            id:0,
                            type:"",
                            name:"",  
                            cron:"",
                            requestUrl:"",
                            requestMethod:"get",
                            requestParams:"",
                            alertEmail:"",
                            scheduleStrategy:0,    
                            scheduleStatus:2,                  
                            description:""
                        };                           
                        this.dialogTitle = "Create New Job";
                        this.dialogMode = 1;
                        this.editDialogFormVisible = true;
                    },
                    editSubmit:function(formName){
                        var othis = this;
                        var apiRequest = (this.dialogMode)?apiJobAdd(this,router.authInfo,JSON.stringify(this.dialogForm)):apiJobUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        this.$refs[formName].validate(function(valid){
                            if(!valid){
                                console.log("error submit");
                                return;
                            }
                            othis.editDialogFormVisible = false;                      
                            httpPost(apiRequest).success(function(res,context){                          
                                checkResult(context,res,false);                         
                                context.loadTableData();
                            }).then(function(status, res, context){checkResult(context,res,true);});
                        });                     
                    },
                    resetForm:function(formName) {
                        this.editDialogFormVisible = false;
                        this.$refs[formName].resetFields();
                    },
                    jobSchedule:function(jobId){
                        var jobScheduleInfo = {id:jobId};
                        var apiRequest = apiJobSchedule(this,router.authInfo,JSON.stringify(jobScheduleInfo));
                        httpPost(apiRequest).success(function(res,context){ 
                            checkResult(context,res,false);     
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    },
                    jobStatusUpdate:function(jobId){
                        var jobStatusInfo = {id:jobId};
                        var apiRequest = apiJobStatusUpdate(this,router.authInfo,JSON.stringify(jobStatusInfo));
                        httpPost(apiRequest).success(function(res,context){ 
                            checkResult(context,res,false);  
                            context.loadTableData();   
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }                         
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        type:item.type,
                        name:item.name,  
                        cron:item.cron,
                        requestUrl:item.requestUrl,
                        requestMethod:item.requestMethod,
                        requestParams:item.requestParams,
                        alertEmail:item.alertEmail,
                        scheduleStrategy:item.scheduleStrategy,       
                        scheduleStrategyName: (item.scheduleStrategy==1)?"Once":"Default",  
                        scheduleStatus:item.scheduleStatus,
                        scheduleStatusName: (item.scheduleStatus==1)?"Started":"Stop",      
                        scheduleType:(item.scheduleStatus==1)?"success":"info",        
                        description:item.description,                     
                        status:item.deleted});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }
    </script>
</body>
</html>
